package com.ayz.gateway.springbootstarter.application;

import cn.hutool.log.Log;

import com.ayz.gateway.sdk.app.AppSdkClientService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;

/**
 * @ClassName GatewaySdkCloseListener
 * @Description TODO:监听Spring容器关闭事件 完成GatewayClientSdk的关闭
 * @Author 快乐的星球
 * @Date 2023/6/3 16:48
 * @Version 1.0
 **/
@Getter
@Setter
public class GatewaySdkCloseListener implements ApplicationListener<ContextClosedEvent> {

    private AppSdkClientService appSdkClientService;

    private Log LOG=Log.get(GatewaySdkCloseListener.class);
    public GatewaySdkCloseListener(AppSdkClientService appSdkClientService) {
        this.appSdkClientService = appSdkClientService;
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        LOG.info("API网关客户端SDK关闭....");
        try {
            this.appSdkClientService.close();
            LOG.info("API网关客户端SDK关闭成功!");
        }catch (Exception ex){
            LOG.error("API网关关闭失败!",ex);
        }
    }
}
